﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.OpenGL;
using Silvermoon.Classes;

namespace Silvermoon.Controls
{
    public class TabPage : INavigationNode, IDisposable
    {
        public static readonly string LabelStyleName = "TitleTab";
        private Label title;
        private Control container;
        internal TabControl tabControl;

        public TabPage()
            : base()
        {
        }

        public TabPage(Label title, Control container)
            : base()
        {
            this.title = title;
            this.Container = container;
        }

        public TabPage(string title, Control container)
            : base()
        {
            this.Title = title;
            this.Container = container;
        }

        public TabPage(string title, FontSprite font, Control container)
            : this(title, container)
        {
            this.TitleFont = font;
        }


        public virtual void Dispose()
        {
            if (container != null)
            {
                container.Dispose();
                container = null;
            }
            if (title != null)
            {
                title.Dispose();
                title = null;
            }
        }

        internal protected Label Label
        {
            get
            {
                if (title == null) title = new Label { AutoSize = true, StyleName = LabelStyleName, Font = SystemFonts.Big, FontScale = 1.5f };
                return title;
            }
            set
            {
                if (title != null) title.Dispose();
                title = value;
            }
        }

        public TabPage Next { get; internal set; }
        public TabPage Previous { get; internal set; }

        /// <summary>
        /// Gets or sets the title of tab page.
        /// </summary>
        public string Title
        {
            get { return Label.Text; }
            set
            {
                if (Label.Text != value)
                {
                    title.Text = value;
                    OnTitleChanged();
                }
            }
        }

        public FontSprite TitleFont
        {
            get { return Label.Font; }
            set
            {
                Label.Font = value;
                OnLabelChanged();
            }
        }


        public Control Container
        {
            get { return container; }
            set
            {
                if (container != value)
                {
                    container = value;
                    OnContainerChanged();
                }
            }
        }

        private void OnContainerChanged()
        {
            if (tabControl != null) tabControl.UpdateItem(this);
        }

        private void OnTitleChanged()
        {
            if (tabControl != null) tabControl.UpdateItem(this);
        }

        private void OnLabelChanged()
        {
            if (tabControl != null) tabControl.UpdateItem(this);
        }


        #region INavigationNode Members

        INavigationNode INavigationNode.Previous
        {
            get { return Previous; }
        }

        INavigationNode INavigationNode.Next
        {
            get { return Next; }
        }


        #endregion
    }
}
